{ config, lib, ... }:
with lib;
let
  cfg = config.roles.server.navidrome;
in {
  options.roles.server.navidrome = {
    enable = mkOption {
      default = false;
      type = types.bool;
    };
    domain = mkOption {
      default = config.roles.server.domain;
      type = types.str;
    };
    musicFolder = mkOption {
      default = "/var/empty";
      type = types.str;
    };
  };

  config = mkIf cfg.enable {
    services = {
      navidrome = {
        enable = true;
        settings.MusicFolder = cfg.musicFolder;
      };

      nginx = {
        upstreams.navidrome.servers = { "${config.services.navidrome.settings.Address}:${toString config.services.navidrome.settings.Port}" = {}; };
        virtualHosts."music.${cfg.domain}" = {
          forceSSL = true;
          useACMEHost = cfg.domain;
          locations."/".proxyPass = "http://navidrome";
        };
      };
    };
  };
}
